<template>
  <div class="login-page">
    <!-- 页面左上角：系统名字 -->
    <header class="global-header">
      <span class="header-title">蒲公英管理系统</span>
    </header>

    <!-- 登录框 -->
    <section class="login-wrap">
      <div class="login-card" :class="{ shake: loginError }">
        <!-- 左上角标题 -->
        <h2 class="card-title">登录</h2>

        <form class="login-form" @submit.prevent="handleLogin">
          <!-- 输入框组：用户名 -->
          <div class="input-group">
            <!-- TODO: 替换为你 iconfont 里的用户图标 -->
            <i class="iconfont icon-user"></i>
            <input v-model="form.username" type="text" placeholder="用户名" required />
          </div>

          <!-- 输入框组：密码 -->
          <div class="input-group">
            <!-- TODO: 替换为你 iconfont 里的密码图标 -->
            <i class="iconfont icon-lock"></i>
            <input v-model="form.password" type="password" placeholder="密码" required />
          </div>

          <!-- 登录按钮 -->
          <button type="submit" class="submit-btn" :disabled="loading">
            {{ loading ? '登录中...' : '登录' }}
          </button>
        </form>
      </div>
    </section>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '@/api/auth'

const router = useRouter()
const loading = ref(false)
const loginError = ref(false) // 控制登录失败时的抖动动画

const form = ref({
  username: '',
  password: ''
})

const handleLogin = async () => {
  if (!form.value.username || !form.value.password) {
    ElMessage.warning('请输入用户名和密码')
    return
  }
  loading.value = true
 try {
  console.log('[Login] 开始登录')
  const res = await login(form.value.username, form.value.password)
  console.log('[Login] 原始返回', res)   // 先看实际拿到什么
  if (res && res.token) {               // 加一层保护
    localStorage.setItem('token', res.token)
    ElMessage.success('登录成功')
    router.push('/')
  } else {
    ElMessage.error('登录接口未返回 token')
  }
} catch (e: any) {
  console.error('[Login] 网络/业务错误', e)   // 关键日志
  ElMessage.error(e?.message || '登录请求失败')
  triggerError()
}
}

// 添加错误动画触发
const triggerError = () => {
  loginError.value = true
  setTimeout(() => {
    loginError.value = false
  }, 400)
}
</script>

<style scoped>
/* ========== 1. 背景：轻微动态渐变 ========== */
.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #3be6c7, #758eff, #ffb5b5, #758eff);
  background-size: 200% 200%;
  animation: gradientMove 10s ease infinite; /* 动态渐变动画 */
  font-family: 'Microsoft YaHei', 'PingFang SC', Arial, sans-serif;
  position: relative;
}

@keyframes gradientMove {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

/* ========== 2. 左上角系统名，渐变字色 ========== */
.global-header {
  position: absolute;
  top: 56px;
  left: 86px;
}
.header-title {
  font-size: 48px;
  font-weight: 700;
  background: linear-gradient(90deg, #ffffff, #fdf8e1, #dff6ff); /* 渐变文字 */
  -webkit-background-clip: text;
  color: transparent;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  letter-spacing: 1px;
}

/* 登录区域居中 */
.login-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ========== 3. 登录卡片，增加内阴影 + 毛玻璃 ========== */
.login-card {
  width: 400px;
  background: rgba(255, 255, 255, 0.9); /* 半透明白色 */
  border-radius: 12px;
  padding: 40px 36px;
  box-shadow:
    0 8px 24px rgba(0, 0, 0, 0.15),
    /* 外阴影 */ inset 0 1px 2px rgba(255, 255, 255, 0.2); /* 内阴影 */
  position: relative;
  backdrop-filter: blur(6px); /* 毛玻璃效果 */
  transition: transform 0.3s;
}

/* 登录失败时的抖动动画 */
.login-card.shake {
  animation: shake 0.4s;
}
@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  20%,
  60% {
    transform: translateX(-8px);
  }
  40%,
  80% {
    transform: translateX(8px);
  }
}

/* 标题在左上角 */
.card-title {
  font-size: 22px;
  font-weight: 500;
  color: #333;
  margin-bottom: 30px;
  text-align: left;
}

/* ========== 4. 输入框组，带图标 ========== */
.input-group {
  display: flex;
  align-items: center;
  border: 1px solid rgba(220, 223, 230, 0.6);
  border-radius: 6px;
  margin-bottom: 28px;
  background: rgba(255, 255, 255, 0.8);
  padding: 0 10px;
  backdrop-filter: blur(4px);
  transition:
    border-color 0.2s,
    box-shadow 0.2s;
}
.input-group i {
  font-size: 18px;
  color: #999;
  margin-right: 8px;
}
.input-group input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  height: 40px;
  font-size: 15px;
}
.input-group:focus-within {
  border-color: #409eff;
  box-shadow: 0 0 6px rgba(64, 158, 255, 0.3);
}

/* ========== 5. 登录按钮优化：渐变 + 点击反馈 ========== */
.submit-btn {
  width: 100%;
  height: 46px;
  background: linear-gradient(135deg, #409eff, #66b1ff); /* 渐变色 */
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  backdrop-filter: blur(4px);
  transition:
    background 0.2s,
    transform 0.1s;
}
.submit-btn:hover {
  background: linear-gradient(135deg, #66b1ff, #409eff);
}
.submit-btn:active {
  transform: scale(0.98); /* 点击缩小反馈 */
}
.submit-btn:disabled {
  background: rgba(160, 207, 255, 0.6);
  cursor: not-allowed;
}
</style>
